/* element-ui */
import { MessageBox } from 'element-ui';
/* i18n */
import i18n from '@src/locales';
/* util */
import { getRootWindow } from '@src/util/dom';

function alert(message: string, title: string, confirmButtonText: string):  Promise<void> {
  return new Promise<void>((resolve, reject) => {
    
    MessageBox.alert(message, title, {
      confirmButtonText,
      callback: action => {
        
        if (action === 'confirm') {
          resolve()
        } else {
          reject()
        }
        
      }
    })
    
  })
}

function commonAlert(message: string): Promise<void> {
  
  const title = i18n.t('common.base.toast') as string
  const confirmButtonText = i18n.t('common.base.makeSure') as string
  
  return alert(message, title, confirmButtonText)
}

function exportAlert(message: string): Promise<void> {
  
  const rootWindow = getRootWindow(window)
  
  const afterHandler = () => {
    rootWindow.showExportList()
    rootWindow.exportPopoverToggle(true)
  }
  
  return (
    commonAlert(message).then(afterHandler).catch(afterHandler)
  )
}

function showExportListAlert(message: string): Promise<void> {
  
  const rootWindow = getRootWindow(window)
  
  const afterHandler = () => {
    rootWindow.showExportList()
    rootWindow.exportPopoverToggle(true)
  }
  
  return (
    commonAlert(message).then(afterHandler).catch(afterHandler)
  )
}

function showExportList() {
  const rootWindow = getRootWindow(window)
  rootWindow.showExportList()
  rootWindow.exportPopoverToggle(true)
}

export {
  commonAlert,
  exportAlert,
  showExportListAlert,
  showExportList
}
